package main

import (
	"common/cfgstruct"
	"common/log"
	"common/process"
	"encoding/json"
	"euplink/config"
	"fmt"
	"github.com/spf13/cobra"
	"os"
	"path/filepath"
)

type Config struct {
	AdminUrl string `help:"卫星接口地址" default:"123.207.34.45:10004"`
	Project  config.Config
	Log      log.Config
}

var (
	configFile string
	rootCmd    = &cobra.Command{
		Use:   "euplink",
		Short: "分布式存储-euplink",
	}
	setupCmd = &cobra.Command{
		Use:         "setup",
		Short:       "初始化配置文件",
		RunE:        cmdSetup,
		Annotations: map[string]string{"type": "setup"},
	}
	configCmd = &cobra.Command{
		Use:   "config",
		Short: "查看当前所有配置",
		RunE:  cmdConfig,
	}
	runConfig        Config
	setupConfig      Config
	listObjectConfig struct {
		Config
		Pre    string `help:"搜索前缀" default:""`
		Cursor string `help:"最后一个" default:""`
	}
)

func main() {

	defaultConfig := os.ExpandEnv("$HOME/.local/euplink.yaml")
	fmt.Println(defaultConfig)
	cfgstruct.SetupFlag(rootCmd, &configFile, "config", defaultConfig, "配置文件")
	//根据环境读取默认配置
	defaults := cfgstruct.DefaultsFlag(rootCmd)
	currentDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
	//程序目录
	rootDir := cfgstruct.ConfigVar("ROOT", currentDir)
	//设置系统的HOME变量
	envHome := cfgstruct.ConfigVar("HOME", os.Getenv("HOME"))
	rootCmd.AddCommand(uploadCmd)
	rootCmd.AddCommand(uploadHlsCmd)
	rootCmd.AddCommand(uploadDirCmd)
	rootCmd.AddCommand(uploadDirFilesCmd)
	rootCmd.AddCommand(downladHlsCmd)
	rootCmd.AddCommand(downloadCmd)
	rootCmd.AddCommand(apikeyCmd)
	rootCmd.AddCommand(listObjectsCmd)
	rootCmd.AddCommand(deleteObjectCmd)
	rootCmd.AddCommand(setupCmd)
	rootCmd.AddCommand(configCmd)
	process.Bind(uploadCmd, &runConfig, defaults, cfgstruct.ConfigFile(configFile), envHome, rootDir)
	process.Bind(uploadHlsCmd, &runConfig, defaults, cfgstruct.ConfigFile(configFile), envHome, rootDir)
	process.Bind(uploadDirCmd, &runConfig, defaults, cfgstruct.ConfigFile(configFile), envHome, rootDir)
	process.Bind(uploadDirFilesCmd, &runConfig, defaults, cfgstruct.ConfigFile(configFile), envHome, rootDir)
	process.Bind(downladHlsCmd, &runConfig, defaults, cfgstruct.ConfigFile(configFile), envHome, rootDir)
	process.Bind(downloadCmd, &runConfig, defaults, cfgstruct.ConfigFile(configFile), envHome, rootDir)
	process.Bind(listObjectsCmd, &listObjectConfig, defaults, cfgstruct.ConfigFile(configFile), envHome, rootDir)
	process.Bind(deleteObjectCmd, &runConfig, defaults, cfgstruct.ConfigFile(configFile), envHome, rootDir)
	process.Bind(apikeyCmd, &runConfig, defaults, cfgstruct.ConfigFile(configFile), envHome, rootDir)
	process.Bind(configCmd, &runConfig, defaults, cfgstruct.ConfigFile(configFile), envHome, rootDir)
	process.Bind(setupCmd, &setupConfig, defaults, cfgstruct.ConfigFile(configFile), envHome, cfgstruct.SetupMode(), rootDir)
	process.Exec(rootCmd)
}

func cmdSetup(cmd *cobra.Command, args []string) error {
	return process.SaveConfig(cmd, configFile)
}
func cmdConfig(cmd *cobra.Command, args []string) error {
	fmt.Printf("当前运行环境：[%s]\n", cfgstruct.DefaultsType())
	fmt.Println("当前配置文件路径：", configFile)
	output, _ := json.MarshalIndent(runConfig, "", " ")
	fmt.Println(string(output))
	return nil
}
